﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
using BISPOS.Repository;
namespace BISPOS
{
    
    public partial class AddBill_SelectSupplierForm : DevExpress.XtraEditors.XtraForm
    {
        AddBillUserControl addBillUsercontrol;
        SupplierRepository supplierRepository = new SupplierRepository();
        List<Supplier> allSuppliers = new List<Supplier>();
        public AddBill_SelectSupplierForm(AddBillUserControl _addBillUserControl)
        {
            InitializeComponent();
            addBillUsercontrol = _addBillUserControl;
            allSuppliers = supplierRepository.AllSupplier().ToList();
            supplierListlinqServerModeSource.QueryableSource = allSuppliers.AsQueryable();
        }

        private void allProductsListBoxControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                this.Text = "เลือกผู้ป้อน - " + allSuppliersListBoxControl.SelectedItems.OfType<Supplier>().FirstOrDefault().Name;
            }
            catch { }
        }

        private void AddBill_SelectSupplierForm_Shown(object sender, EventArgs e)
        {
            searchTextEdit.Focus();
        }

        private void searchTextEdit_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up)
            {
                if (allSuppliersListBoxControl.SelectedIndex != 0)
                {
                    try { allSuppliersListBoxControl.SelectedIndex -= 1; }
                    catch { }
                }
            }
            if (e.KeyCode == Keys.Down)
            {
                try { allSuppliersListBoxControl.SelectedIndex += 1; }
                catch { }
            }
            if (e.KeyCode == Keys.Enter)
            {
                try
                {
                    this.addBillUsercontrol.AddSupplier(allSuppliersListBoxControl.SelectedItems.OfType<Supplier>().FirstOrDefault());
                    this.Close();
                }
                catch
                {

                }
            }
            if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
        }

        private void searchTextEdit_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                supplierListlinqServerModeSource.QueryableSource = allSuppliers.Where(x => x.Name.Contains(searchTextEdit.Text)).AsQueryable();
                supplierListlinqServerModeSource.Reload();
            }
            catch { }
        }


    }
}